package com.hoperun.controller.admin;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hoperun.bean.AdminOrganizations;
import com.hoperun.controller.common.BaseController;
import com.hoperun.framework.ex.XHZException;
import com.hoperun.framework.ibatis.page.BootGrid;
import com.hoperun.framework.ibatis.page.BootGridPage;
import com.hoperun.framework.models.DataView;
import com.hoperun.framework.query.Criteria;
import com.hoperun.framework.util.Constant;
import com.hoperun.service.IAdminOrganizationsSV;
import com.hoperun.utils.ErrorCode;
import com.hoperun.utils.Constant.RecordState;

/**
 * Copyright: Copyright (c) 2016 GUO~AN <br/>
 * ClassName: AdminOrganizationsController.java
 * 
 * @Description:
 * 
 * @version: v1.0.0
 * @author: fuyu
 * @date: 2016-1-8 上午9:11:06
 * 
 *        Modification History: Date Author Version Description
 *        ---------------------------------------------------------* 2016-1-8
 *        fuyu v1.0.0 修改理由
 */
@Controller
@RequestMapping("/admin/admin/organizations")
@RequiresUser
public class AdminOrganizationsController extends BaseController {

	@Resource
	private IAdminOrganizationsSV adminOrganizationsSV;

	@ModelAttribute
	public void populateModel(final HttpServletRequest request,
			ModelMap modelMap) {
		try {
			super.initMenus(request, modelMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/{menu}/{menuNext}")
	public String index(@PathVariable long menu, @PathVariable long menuNext,
			ModelMap modelMap) {

		modelMap.put("menuType", menu);
		modelMap.put("menuNextType", menuNext);

		return "admin:adminorganizations.list";
	}

	@RequestMapping("/tree")
	@ResponseBody
	public DataView organizationTree(
			@RequestParam(value = "orgId", defaultValue = "0") Long pid) {

		Criteria criteria = new Criteria();
		DataView dataView = new DataView() ;
		try {
			if (null == pid || pid < 1) {
				criteria.addNull(AdminOrganizations.COL_PARENT_ID, Criteria.AND);
			} else {
				criteria.addEqual(AdminOrganizations.COL_PARENT_ID, pid,
						Criteria.AND);
			}

			criteria.orderBy(AdminOrganizations.COL_SORT + " DESC ");
			List<AdminOrganizations> list = adminOrganizationsSV
					.queryByCondition(criteria);
			if (CollectionUtils.isNotEmpty(list)) {
				dataView.setCode(Constant.SUCCESS);
				dataView.setData(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return dataView;
	}

	@RequestMapping("/page")
	@ResponseBody
	public BootGridPage<AdminOrganizations> identityUserPage(
			BootGrid<AdminOrganizations> grid,
			AdminOrganizations adminOrganizations, ModelMap modelMap) {
		BootGridPage<AdminOrganizations> pageModel = new BootGridPage<AdminOrganizations>();

		try {
			Criteria criteria = new Criteria();

			if (null == grid)
				return new BootGridPage<AdminOrganizations>();

			pageModel = adminOrganizationsSV.queryPage(criteria, grid);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return pageModel;
	}

	@RequestMapping("/create")
	public String create(AdminOrganizations adminOrganizations,
			ModelMap modelMap) {
		try {
			modelMap.addAttribute("method", "save");

			Criteria criteria = new Criteria();
			criteria.addEqual(AdminOrganizations.COL_PARENT_ID,
					adminOrganizations.getParentId(), Criteria.AND);
			criteria.addEqual(AdminOrganizations.COL_STATUS,
					RecordState.STS_USE, Criteria.AND);
			criteria.orderBy(AdminOrganizations.COL_SORT + " DESC ");
			criteria.limit(Constant.ZERO, Constant.ONE);

			List<AdminOrganizations> orgs = adminOrganizationsSV
					.queryByCondition(criteria);

			if (CollectionUtils.isEmpty(orgs)) {
				adminOrganizations.setSort(Constant.ONE);
			} else {
				adminOrganizations.setSort(Constant.ONE
						+ orgs.get(Constant.ZERO).getSort());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		modelMap.addAttribute("row", adminOrganizations);
		return "adminAlone:adminorganizations.info";
	}

	@RequestMapping("/modify/{id}")
	public String modify(@PathVariable Long id, ModelMap modelMap,
			final HttpServletRequest request) {
		AdminOrganizations adminOrganizations = null;
		try {
			adminOrganizations = adminOrganizationsSV.queryById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		modelMap.addAttribute("method", "update");
		modelMap.addAttribute("row", adminOrganizations);
		return "adminAlone:adminorganizations.info";
	}

	@RequestMapping("/detail/{id}")
	public String detail(@PathVariable Long id, ModelMap modelMap,
			final HttpServletRequest request) {
		AdminOrganizations adminOrganizations = null;
		try {
			adminOrganizations = adminOrganizationsSV.queryById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		modelMap.addAttribute("detail", adminOrganizations);
		return "adminAlone:adminorganizations.detail";
	}

	@RequestMapping("/save")
	@ResponseBody
	public DataView save(AdminOrganizations adminOrganizations) {
		DataView dataView = new DataView();

		try {
			adminOrganizationsSV.save(adminOrganizations);

			dataView.setCode(Constant.SUCCESS);
			dataView.setMessage("新增成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataView;
	}

	@RequestMapping("/update")
	@ResponseBody
	public DataView update(AdminOrganizations adminOrganizations) {
		DataView dataView = new DataView();

		if (null == adminOrganizations
				|| StringUtils.isBlank("" + adminOrganizations.getOrgId())) {
			throw new XHZException(ErrorCode.MISS_PARAM, null,
					new Object[] { "id信息" });
		}

		try {
			adminOrganizationsSV.update(adminOrganizations);

			dataView.setCode(Constant.SUCCESS);
			dataView.setMessage("修改成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataView;
	}

	@RequestMapping("/find/{id}")
	@ResponseBody
	public AdminOrganizations findOne(@PathVariable Long id) {
		AdminOrganizations adminOrganizations = null;
		try {
			adminOrganizations = adminOrganizationsSV.queryById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return adminOrganizations;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public DataView delete(Long id) {
		DataView dataView = new DataView();
		try {

			dataView = new DataView();

			adminOrganizationsSV.delete(id);

			dataView.setCode(Constant.SUCCESS);
			dataView.setMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			throw new XHZException(e.getMessage());
		}

		return dataView;
	}
}
